/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Admin;
import model.Customer;
import model.Feedback;

/**
 *
 * @author Administrator
 */
public class UserDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;

    public Admin loginAdmin(Admin admin) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_LoginAdmin(?,?)}");
            cs.setString("username", admin.getUsername());
            cs.setString("password", admin.getPassword());

            ResultSet rs = cs.executeQuery();
            if (rs.next()) {
                admin = new Admin(rs.getInt("AdminID"), rs.getString("AdminName"), null, null);
                return admin;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Customer loginCustomer(Customer customer) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_LoginCustomer(?,?)}");
            cs.setString("username", customer.getUsername());
            cs.setString("password", customer.getPassword());

            ResultSet rs = cs.executeQuery();
            if (rs.next()) {
                customer = new Customer(rs.getInt("CustomerID"), rs.getString("UserName"), null, rs.getString("CustomerName"), rs.getString("Email"), rs.getString("PhoneNumber"), rs.getString("Address"));
                return customer;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public boolean register(Customer customer) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_Register(?,?,?,?,?,?)}");
            cs.setString("username", customer.getUsername());
            cs.setString("password", customer.getPassword());
            cs.setString("customername", customer.getCustomerName());
            cs.setString("email", customer.getEmail());
            cs.setString("phonenumber", customer.getPhoneNumber());
            cs.setString("address", customer.getAddress());
            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean customerFeedback(int cusID, String title, String textContent) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_Feedback(?,?,?)}");
            cs.setInt("cusID", cusID);
            cs.setString("title", title);
            cs.setString("content", textContent);
            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public List<Feedback> listAllFeedback(){
        try {
            List<Feedback> list = new ArrayList<Feedback>();
            Feedback feedback;
            cs = conn.createConnection().prepareCall("{call sp_ListAllFeedback}");
            ResultSet rs = cs.executeQuery();
            while(rs.next()){
                feedback = new Feedback(rs.getInt("FeedbackID"),rs.getInt("CustomerID"),rs.getString("Title"),rs.getString("TextContent"),rs.getString("FeedbackDay"));
                list.add(feedback);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

